package com.sxf.core;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.sxf.core.interceptor.TokenInterceptor;
import com.sxf.core.record.RecordInterceptor;
import com.sxf.core.record.dao.SourceRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author ccccit
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	@Autowired
	private SourceRecordMapper sourceRecordMapper;
	
	@Autowired
    private TokenInterceptor tokenInterceptor;


	private static final List<String> COMMON_EXCLUDE_PATH = CollectionUtil.newArrayList(
			"/",
			"/doc.html",
			"/swagger-resources/**",
			"/webjars/**",
			"/v2/**",
			"/swagger/code",
			"/swagger-ui.html/**",
			"/error",
			"/favicon.ico"
	);

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new RecordInterceptor(sourceRecordMapper));
		
		// 用户token拦截
		addTokenInterceptor(registry);
//
//		// 判断用户状态，如果冻结或者注销状态，拦住！
//		addUserStatusInterceptor(registry);
	}

//	private void addUserStatusInterceptor(InterceptorRegistry registry) {
//        registry.addInterceptor(userStatusInterceptor).addPathPatterns("/**")
//        .excludePathPatterns(excludePathPatterns());
//    }
	
	private void addTokenInterceptor(InterceptorRegistry registry) {
	    registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
        .excludePathPatterns(excludePathPatterns());
    }

    @Autowired
	private ObjectMapper objectMapper;

	@PostConstruct
	public void initObjectMapper() {
		SimpleModule simpleModule = new SimpleModule();
		simpleModule.addSerializer(Long.class, new PrimaryKeySerial());
		objectMapper.registerModule(simpleModule);
	}

	public List<String> excludePathPatterns(){
		List<String> excludePathList = CollectionUtil.newArrayList(
				"/pass/**",
				"/sms/**",
				"/captcha/**",
                "/attach/**",
				"/op/settle/export"
				);
		excludePathList.addAll(COMMON_EXCLUDE_PATH);
		return excludePathList;
	}
}